﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Javavaj.iHunter.UI
{
    internal partial class MessageViewForm : DevComponents.DotNetBar.Office2007Form
    {
        public MessageViewForm()
        {
            InitializeComponent();
            ShowMessage(null, null);
        }

        Control contentViewerControl = null;

        public void ShowMessage(IViewer viewer, IMessage message)
        {
            if (message == null)
            {
                this.Text = string.Empty;
                this.panelHeader.Visible = false;
                this.panelContentViewer.Visible = false;
                this.panelAttachmentList.Visible = false;
                return;
            }
            this.panelHeader.Visible = true;
            this.panelContentViewer.Visible = true;
            this.panelAttachmentList.Visible = (message.Attachments.Count != 0);

            this.Text = message.Title;
            this.Parent.Update();


            ShowHeaders(message);

            ShowContent(viewer, message);


            ShowAttachments(message);


        }

        private void ShowAttachments(IMessage message)
        {
            foreach (DevComponents.DotNetBar.BaseItem item in panelAttachmentList.Items)
            {
                item.Dispose();
            }
            this.panelAttachmentList.Items.Clear();

            panelAttachmentList.BeginUpdate();

            //Show attachments
            foreach (IFileObject attachment in message.Attachments)
            {
                IFileObject file = attachment;
                DevComponents.DotNetBar.ButtonItem button = new DevComponents.DotNetBar.ButtonItem();
                button.Tag = attachment;
                button.ButtonStyle = DevComponents.DotNetBar.eButtonStyle.ImageAndText;
                button.ImagePosition = DevComponents.DotNetBar.eImagePosition.Left;
                button.Shape = DevComponents.DotNetBar.RoundRectangleShapeDescriptor.RectangleShape;
                button.Cursor = Cursors.Hand;
                button.ColorTable = DevComponents.DotNetBar.eButtonColor.Magenta;

                if (System.IO.File.Exists(attachment.LocalPath))
                    button.ImageIndex = 0;
                else
                    button.ImageIndex = 1;
                button.Text = "<a>" + attachment.Filename + "(" + (attachment.FileSize / 1024).ToString() + "KB)</a>: " + attachment.Description;


                button.Click += 
                    delegate
                    {
                        if (AttachmentLinkClicked != null)
                            AttachmentLinkClicked.Invoke(this, new AttachmentLinkClickedEventArgs(file));
                    };
                button.Visible = true;
                panelAttachmentList.Items.Add(button);

            }
            panelAttachmentList.Refresh();
            panelAttachmentList.EndUpdate();
        }



        private void ShowContent(IViewer viewer, IMessage message)
        {
            //Show content
            if (contentViewerControl != null)
            {
                contentViewerControl.Hide();
                this.panelContentViewer.Controls.Remove(contentViewerControl);

                contentViewerControl.Dispose();
                contentViewerControl = null;
            }
            contentViewerControl = viewer.CreateControl();

            if (viewer == null || viewer.DocumentTypes == null || 
                !viewer.DocumentTypes.Contains(message.ContentType) ||
                contentViewerControl == null)
            {
                this.textDescription.Visible = true;
                this.textDescription.Dock = DockStyle.Fill;
                this.textDescription.Text = labelNormalText.Text +
                    Environment.NewLine +
                    Environment.NewLine +
                    message.Description;
            }
            else
            {
                this.textDescription.Visible = false;
                this.panelContentViewer.Controls.Add(contentViewerControl);
                contentViewerControl.Dock = DockStyle.Fill;
                contentViewerControl.Show();

                if (message.ContentIsBinary)
                {
                    using (System.IO.Stream stream = new System.IO.MemoryStream(message.Content as byte[]))
                    {
                        viewer.ShowDocument(contentViewerControl,message.ContentType,stream);

                    }

                }
                else
                {
                    using (System.IO.Stream stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes((message.Content as string))))
                    {
                        viewer.ShowDocument(contentViewerControl,message.ContentType, stream);

                    }
                }


            }
        }

        private void ShowHeaders(IMessage message)
        {

            //Show header
            this.labelX1.Visible = true;
            this.labelAuthor.Visible = (message.Author.Length > 0);
            this.labelDueTime.Visible = (!message.DueTime.Equals(DateTime.MinValue));
            this.labelImportance.Visible = true;
            this.labelPubTime.Visible = (!message.PubTime.Equals(DateTime.MinValue));
            this.labelUpdateTime.Visible = (!message.LastBuildTime.Equals(DateTime.MinValue));
            if (string.IsNullOrEmpty(message.Title))
                this.labelX1.Text = "<font size=\"+3\"><b>" + labelUntitledText.Text + "</b></font>";
            else
                this.labelX1.Text = "<font size=\"+3\"><b>" + message.Title + "</b></font>";
            this.labelAuthor.Text = labelAuthorText.Text + message.Author;
            this.labelDueTime.Text = labelDuetimeText.Text + message.DueTime.ToString("f", System.Globalization.CultureInfo.CurrentUICulture);
            this.labelImportance.Text = labelImportanceText.Text + ((message.Importance == 0) ? (labelNormalText.Text) : (labelImportantText.Text));
            this.labelPubTime.Text = labelPubtimeText.Text + message.PubTime.ToString("f", System.Globalization.CultureInfo.CurrentUICulture);
            this.labelUpdateTime.Text = this.labelUpdatetimeText.Text + message.LastBuildTime.ToString("f", System.Globalization.CultureInfo.CurrentUICulture);
            panelHeader.PerformLayout();
        }
        public event AttachmentLinkClickedEventHandler AttachmentLinkClicked;

    

        private void panelContentViewer_Resize(object sender, EventArgs e)
        {
        }

        private void MessageViewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
                e.Cancel = true;
            this.Hide();
        }
    }
    internal class AttachmentLinkClickedEventArgs : EventArgs
    {
        IFileObject attachment;
        public IFileObject Attachment
        {
            get
            {
                return attachment;
            }
        }

        public AttachmentLinkClickedEventArgs(IFileObject attachment)
            : base()
        {
            this.attachment = attachment;

        }
    }
    internal delegate void AttachmentLinkClickedEventHandler(object sender, AttachmentLinkClickedEventArgs e);
}
